package com.qfedu.edu.seckill.storageservice.queue.listener;

import com.qfedu.edu.seckill.storageservice.service.StockStorageService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author xiaobobo
 * @title: OrderQueueListener
 * @projectName cd-java-2404-seckill-parent
 * @description: 这个类主要用来接受来自队列的消息
 * @date 2025/2/6  14:15
 */
@Component
@Slf4j
public class ReduceQueueListener {


    @Autowired
    private StockStorageService stockStorageService;


    /**
     * 这里是接受来自订单队列的消息
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {"reduceQueue"})
    public void receive(byte[] data, Message message, Channel channel) throws IOException {
        String msg = new String(data, "UTF-8");
        //减库存
        stockStorageService.reduce(msg);
        //手动确认 告诉我们的队列收到这个消息了(然后队列就会将这个消息给删除掉)
      channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }


}
